<?php

namespace App\Http\Controllers;

use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Http\Request;
use App\Http\model\Role;

class RoleController extends Controller
{

	/**
	 * [addRole 角色添加]
	 * @Author   孙丽男
	 * @DateTime 2018-06-05
	 * @param    [角色名] str $name
	 * @param    [父级id] int $pid
	 * @param    [备注] str $comment  非必须
	 * @version  6.2
	 */
	public function addRole(Request $request){

		$result = array();

    	if ($request['name'] && $request['pid'] || $request['pid']=='0') {
    		
    		$roleArr = $request->all();
            if (isset($roleArr['s'])) {
                $roleArr = array_except($roleArr, ['s']);
            }
    		$role = new Role;

    		$result = $role->addRoleModel($roleArr);

    		return $result;

    	}else{

    		$result['code'] = 1002;
    		$result['msg'] = '参数为空';
    		return json_encode($result);

    	}

	}

	/**
	 * [findRole 角色列表]
	 * @Author   孙丽男
	 * @DateTime 2018-06-05
	 * @version  6.2
	 * @return   [json]   $result
	 */
	public function findRole(Request $request){
		
		//构建查询条件
    	$where = array();

        if ($request['roleId']) {
            
            $where = [
                ['roles.id', $request['roleId']],
            ]; 
        }

    	if ($request['name']) {
    		
    		$where[1] = ['roles.name', 'like', '%' . $request['name'] . '%'];
    	}

    	if ($request['start_time']) {
    		
    		$where[2] =  ['roles.update_time', '>',  $request['start_time']];
    	}

    	if ($request['end_time']) {
    		
    		$where[3] =  ['roles.update_time', '<',  $request['end_time']];
    	}

    	$role = new Role;

    	$result = $role->findRoleModel($where);

    	return $result;
	}

	/**
	 * [deleteRole 删除角色]
	 * @Author   孙丽男
	 * @DateTime 2018-06-05
	 * @param    [角色id] array $roleId[]
	 * @version  6.2
	 * @return   [json]   $result
	 */
	public function deleteRole(Request $request){

		$result = array();
   
    	if (is_array($request['roleId'])) {

    		$roleArr = $request->all();
            if (isset($roleArr['s'])) {
                $roleArr = array_except($roleArr, ['s']);
            }
    		$roleIdArr = $roleArr['roleId'];

    		$role = new Role;

    		$result = $role->deleteRoleModel($roleIdArr);

    		return $result;

    	}else{

    		$result['code'] = 1002;
    		$result['msg'] = '参数为空';
    		return json_encode($result);
    	}
	}

	/**
	 * [updateRole 修改角色]
	 * @Author   孙丽男
	 * @DateTime 2018-06-05
	 * @param    [角色id]  int $roleId
     * @param    [修改的信息]  ....
	 * @version  6.2
	 * @return   [json]   $result
	 */
	public function updateRole(Request $request){

		$roleId = $request['roleId'];

    	if ($roleId) {
    		
    		$updateInfo = $request->all();
            if (isset($updateInfo['s'])) {
                $updateInfo = array_except($updateInfo, ['s']);
            }
    		unset($updateInfo['roleId']);

    		if ($updateInfo) {
                
                $role = new Role;
	            $result = $role->updateRoleModel($roleId, $updateInfo);
	            return $result;

            }else{

                $result['code'] = 1002;
                $result['msg'] = '请填写修改数据';
                return json_encode($result);
            }

    	}else{

    		$result['code'] = 1002;
    		$result['msg'] = '参数为空';
    		return json_encode($result);
    	}
	}

	/**
     * [powerList 权限树状列表]
     * @Author   孙丽男
     * @DateTime 2018-06-05
     * @version  6.2
     * @return   [josn]     [$result]
     */
    public function powerList(){
            
            $role = new Role;
            $result = $role->powerListModel();
            return $result;
    }

	/**
	 * [setPermissions 为角色设置权限]
	 * @Author   孙丽男
	 * @DateTime 2018-06-05
     * @param    [角色id] array $roleId[]
     * @param    [权限id] array $powerId[]
	 * @version  6.2
	 */
	public function setPower(Request $request){

		 $result = array();

        if (is_array($request['roleId']) && is_array($request['powerId'])) {
            
            $role = new Role;

            $result = $role->setPowerModel($request['roleId'], $request['powerId']);

            return $result;

        }else{

            $result['code'] = 1002;
            $result['msg'] = '参数为空';
            return json_encode($result);
        }
	}
}
